package com.jackiechancinema.ssm.service.Impl;


import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.ContractBo;
import com.jackiechancinema.ssm.bo.CustomerBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.ClassDao;
import com.jackiechancinema.ssm.dao.CustomerDao;
import com.jackiechancinema.ssm.dto.ContractExcelDto;
import com.jackiechancinema.ssm.dto.CustomerDto;
import com.jackiechancinema.ssm.dto.CustomerExcelDto;
import com.jackiechancinema.ssm.entity.Class;
import com.jackiechancinema.ssm.entity.Contract;
import com.jackiechancinema.ssm.entity.Customer;
import com.jackiechancinema.ssm.listener.ContractReadListener;
import com.jackiechancinema.ssm.listener.CustomerReadListener;
import com.jackiechancinema.ssm.service.Inte.CustomerService;
import com.jackiechancinema.ssm.utils.BeanUtil;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private CustomerReadListener listener;

    //新增
    @Override
    public R insert(Customer customer) {
        if (customerDao.insert(customer)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }

    }
    //查询
    @Override
    public PageVo<CustomerDto> queryPage(CustomerBo customerBo) {
        if (customerBo != null){
            Customer customer = new Customer();
            customer.setCustomer_id(customerBo.getCustomer_id());
            customer.setCustomer_name(customerBo.getCustomer_name());
            customer.setCustomer_mobile(customerBo.getCustomer_mobile());
            customer.setClass_id(customerBo.getClass_id());
            PageHelper.startPage(customerBo.getPage(), customerBo.getLimit());//开启分页
            PageInfo<CustomerDto> pageInfo = new PageInfo<CustomerDto>(customerDao.selectAll(customer));
            return new PageVo<CustomerDto>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        }
        return new PageVo<CustomerDto>(SystemContant.PAGE_R,"OK",0,null);
    }
    //删除
    @Override
    public R deleteById(int id) {
        if (customerDao.delete(id)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }
    //批量删除
    @Override
    public R deleteBatch(List<CustomerDto> customerDtos) {

        List<Integer> list = new ArrayList<>();
        for (CustomerDto dto : customerDtos) {
            list.add(dto.getCustomer_id());
        }

        if (customerDao.batchDel(list)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }
    @Override
    public R update(Customer customer) {
        if (customerDao.update(customer)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }


    @Override
    public R insertBatch(MultipartFile file) {
        if (!file.isEmpty()){
            try {
                EasyExcel.read(file.getInputStream(), CustomerExcelDto.class,listener).sheet().doRead();
                return R.ok("OK");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public void exportExcel(CustomerBo bo, HttpServletResponse response) {
        //1.设置响应消息头，目的：告诉请求方，这个是下载
        String fn=System.currentTimeMillis()+".xlsx";
        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+fn);

        //2.组装查询条件
        Customer customer=new Customer();
        if(bo.getCustomer_id() != null){
            customer.setCustomer_id(bo.getCustomer_id());
        }
        if(StringUtils.hasLength(bo.getCustomer_name())){
            customer.setCustomer_name(bo.getCustomer_name());
        }
        if(StringUtils.hasLength(bo.getCustomer_mobile())){
            customer.setCustomer_mobile(bo.getCustomer_mobile());
        }
        if(bo.getClass_id() != null){
            customer.setClass_id(bo.getClass_id());
        }
        //3.获取要下载的数据
        List<CustomerDto> list=customerDao.selectAll(customer);
        try {
            //4.生成Excel，并将数据写出到响应流中
            EasyExcel.write(response.getOutputStream(),CustomerDto.class).sheet().doWrite(list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
